package com.humngyang.com.hmy.service;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.humngyang.com.hmy.entity.User;
import com.humngyang.com.hmy.mapper.UserMapper;
import com.humngyang.com.hmy.utils.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.*;

@Service("userservice")
public class UserService {


    @Autowired
    UserMapper userMapper;


    public Map<String, Object> getUserOne(User user) {
        User userentity = userMapper.selectOne(new QueryWrapper<User>()
                .eq("username", user.getUsername())
                .eq("password", user.getPassword()));
        Map<String, Object> ret = new HashMap<>();
        if (userentity != null) {
            ret.put("flag", 1);
            ret.put("token", user.getUsername() + UUID.randomUUID().toString().replace("-", ""));
            return ret;
        }
        ret.put("message", "用户名或者密码错误");
        ret.put("flag", 0);
        return ret;
    }


//    public void selectPage() {
//        Page<Artical> page = new Page<>(1, 2);
//        IPage<Artical> articalIPage = articalMapper.selectPage(page, null);
//        System.out.println("总条数====" + articalIPage.getTotal());
//        System.out.println("当前页数====" + articalIPage.getCurrent());
//        System.out.println("分页查询内容====" + articalIPage.getRecords());
//        System.out.println("当前每页显示数====" + articalIPage.getPages());
//    }
//    QueryWrapper<User> queryWrapper = new QueryWrapper<User>();
//    //QueryWrapper<User> query = Wrappers.<User>query();
//        queryWrapper.like("name","雨").lt("age",40);

    public Map<String, Object> getUserListPage(Map<String, Object> map) {
        Integer pagesize = (Integer) map.get("pagesize");
        Integer current = (Integer) map.get("current");
        String username = (String) map.get("username");
        String email = (String) map.get("email");
        String phone = (String) map.get("phone");

        QueryWrapper<User> queryWrapper = new QueryWrapper<User>();
        if (!StringUtils.isEmpty(username)) {
            queryWrapper.like("username", username);
        }
        if (!StringUtils.isEmpty(email)) {
            queryWrapper.like("email", email);
        }
        if (!StringUtils.isEmpty(phone)) {
            queryWrapper.like("phone", phone);
        }
        if (queryWrapper == null) {
            queryWrapper = null;
        }
        Page<User> page = new Page<>(current, pagesize);
        IPage<User> users = userMapper.selectPage(page, queryWrapper);
        System.out.println("总条数====" + users.getTotal());
        System.out.println("当前页数====" + users.getCurrent());
        System.out.println("分页查询内容====" + users.getRecords());
        System.out.println("当前每页显示数====" + users.getPages());
        Map re = new HashMap();
        if (users.getTotal() == 0) {
            re.put("list", Collections.EMPTY_LIST);
            re.put("flag", 1);
            return re;
        }
        re.put("list", users);
        re.put("flag", 1);
        return re;
    }

    public Map<String, Object> addOrUpdateUser(User user) {
        Map<String, Object> map = new HashMap<>();
        String username = user.getUsername();
        User username1 = userMapper.selectOne(new QueryWrapper<User>().eq("username", username));
        if (StringUtils.isEmpty(user.getId()) && username1 != null) {
            map.put("flag", 0);
            map.put("message", "该用户已添加");
            return map;
        }

        if (StringUtils.isEmpty(user.getId())) {
            user.setId(UUID.randomUUID().toString().replace("-", ""));
            user.setCreatedate(DateUtil.format(new Date(), "yyyy-MM-dd"));
            user.setCreatetime(new Date());
            userMapper.insert(user);
            map.put("flag", 1);
            return map;
        }

        user.setUpdatetime(new Date());
        userMapper.updateById(user);
        map.put("flag", 1);
        return map;
    }

    /**
     * Author : hmy
     * @date 2020/2/23 15:06
     * describution : 根据id查找用户信息
     **/

    public Map<String, Object> getUserById(String id) {
        Map<String, Object> map = new HashMap<>();
        User user1 = userMapper.selectById(id);
        map.put("flag", 1);
        if (user1 != null) {
            map.put("user", user1);
        } else {
            map.put("user", Collections.EMPTY_MAP);
        }
        return map;
    }


    /**
     * Author : hmy
     *
     * @date 2020/2/23 20:38
     * describution : 根据id删除用户信息
     **/

    public Map<String, Object> deleteUserById(String id) {
        Map<String, Object> map = new HashMap<>();
        int i = userMapper.deleteById(id);
        if (i > 0) {
            map.put("flag", 1);
        } else {
            map.put("flag", 0);
            map.put("message", "删除数据错误");
        }
        return map;
    }


}

